module.exports = function ($scope, $resource, $modal, toaster, str2Data) {

	//机构编号查询其游团列表
	$scope.search = function() {
		if(!$scope.searchform.code){
			return;
		}
		$scope.searchform.loading = true;
		$scope.groupList = null;
		$resource('/api/ac/atc/auditReportService/getGroupList', {}, {}).save({'company_code': $scope.searchform.code}, function(res){
            if (res.errcode === 0) {
				$scope.groupList = res.data;
				if($scope.groupList && $scope.groupList.length != 0){
					calcMoney($scope.groupList);
				}
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	};

	//加载参加招徕奖励活动的机构名单
	$scope.loadCompany = function() {	
		$resource('/api/as/atc/tourist/companySimList', {}, {}).save({}, function(res){
            if (res.errcode === 0) {
				$scope.searchform.company_list = res.data;
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	}

	init();
	//初始化几页数据并加载补贴规则
    function init() {
		$scope.searchform = {};
		$scope.total = {};
		$scope.loadCompany();
		//加载完补贴规则后查询游团信息
		$resource('/api/as/atc/policy/getStandardList', {}, {}).save({}, function(res){
			if (res.errcode === 0) {
				$scope.ruleList = res.data;
				angular.forEach($scope.ruleList, function(item) {
					var index = item.subsidy_money.indexOf(',');
					if(index != -1){
						str2Data(item.subsidy_money.substring(0, index) , item);
						str2Data(item.subsidy_money.substring(index + 1) , item);
					} else {
						str2Data(item.subsidy_money, item);
					}
				});
			} else {
				toaster.error({title: '', body: '加载补贴规则失败, 请刷新页面'});
			}
		});
	};

	//查询此机构的补贴规则,然后计算每个团的补贴金额
	function calcMoney(list) {
		$resource('/api/ac/atc/companyService/getSubsidyRule', {}, {}).save({'company_code': $scope.searchform.code}, function(res){
            if (res.errcode === 0) {
				var a = res.data;
				$scope.total.num = 0;
				$scope.total.money = 0;
				//将此机构的补贴规则转换成二元一次方程(1_1100+2_2200 → x*1100+y*2200)
				if(a.subsidy_one){
					$scope.total.subsidy_one = a.subsidy_one;
					$scope.total.rule_one = a.subsidy_one.replace('1_', 'x*');
					$scope.total.rule_one = $scope.total.rule_one.replace('2_', 'y*');
				}
				if(a.subsidy_more){
					$scope.total.subsidy_more = a.subsidy_more;
					$scope.total.rule_more = a.foreigner_subsidy.replace('1_', 'x*');
					$scope.total.rule_more = $scope.total.rule_more.replace('2_', 'y*');
				}
				var x, y;
				angular.forEach(list, function(item) {
					$scope.total.num += item.domestic_num + item.foreigner_num;
					x = item.domestic_num;
					y = item.foreigner_num;
					if(item.group_num >= 3){
						if($scope.total.rule_more){
							item.money = eval($scope.total.rule_more);
							$scope.total.money += item.money;
						}
					} else {
						if($scope.total.rule_one){
							item.money = eval($scope.total.rule_one);
							$scope.total.money += item.money;
						}
					}
				});
				//数据加载并计算完毕
				$scope.searchform.loading = false;
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	}

    //游团信息
	$scope.getGroupInfo = function(info){
		var modalInstance = $modal.open({
			template: require('../views/groupInfo.html'),
			controller: 'groupInfo',
			size: 'lg',
			resolve: {
				info: function () {
					return info;
				},
				subsidyInfo: function () {
					return $scope.total;
				}
			}
		});
    };

    //人员名单
	$scope.getPersonList = function(info) {
		var modalInstance = $modal.open({
			template: require('../views/groupTouristInfo.html'),
			controller: 'groupTouristInfo',
			size: 'lg',
			resolve: {
				info: function () {
					return info;
				}
			}
		});
    };

	//查看补贴规则
	$scope.getSubsidyType = function() {
		var modalInstance = $modal.open({
			template: require('../views/subsidyType.html'),
			controller: 'subsidyType',
			size: 'lg',
			resolve: {
				ruleList: function () {
					return $scope.ruleList;
				}
			}
		});
	}

	//生产审计报告
	$scope.createReport = function(){
		if($scope.groupList && $scope.groupList.length != 0){
			if(confirm('确认游团信息无误, 立即生成审计报告?')){
				// var list = [];
				// angular.forEach($scope.groupList, function(item) {
				// 	list.push(item.code);
				// });
				var para = {
					'company_code': $scope.searchform.code
				}
				$resource('/api/ac/atc/auditReportService/createReport', {}, {}).save(para, function(res) {
					if (res.errcode === 0) {
						toaster.success({title: '', body: '生成审计报告成功'});
						$scope.search();
					} else {
						toaster.error({title: '', body: res.errmsg});
					}
				});
			}
		} else {
			toaster.warning({title: '', body: '请先确认游团信息准确无误'});
		}
	}


};